<?php

namespace App\Containers\AppSection\Accreditation\UI\API\Controllers;

use App\Containers\AppSection\Accreditation\Actions\CheckAccreditationAction;
use App\Containers\AppSection\Accreditation\Actions\GetAccreditationAction;
use App\Containers\AppSection\Accreditation\Actions\GetModeratorAccreditationsAction;
use App\Containers\AppSection\Accreditation\Actions\GetProfileAccreditationsAction;
use App\Containers\AppSection\Accreditation\Actions\ModeratorCheckAccreditationAction;
use App\Containers\AppSection\Accreditation\Actions\SendToAccreditationAction;
use App\Containers\AppSection\Accreditation\Actions\UpdateAccreditationAction;
use App\Containers\AppSection\Accreditation\UI\API\Requests\CheckAccreditationRequest;
use App\Containers\AppSection\Accreditation\UI\API\Requests\GetAccreditationRequest;
use App\Containers\AppSection\Accreditation\UI\API\Requests\GetModeratorAccreditationsRequest;
use App\Containers\AppSection\Accreditation\UI\API\Requests\GetProfileAccreditationsRequest;
use App\Containers\AppSection\Accreditation\UI\API\Requests\ModeratorCheckAccreditationRequest;
use App\Containers\AppSection\Accreditation\UI\API\Requests\SendToAccreditationRequest;
use App\Containers\AppSection\Accreditation\UI\API\Requests\UpdateAccreditationRequest;
use App\Containers\AppSection\Accreditation\UI\API\Transformers\AccreditationTransformer;
use App\Ship\Parents\Controllers\ApiController;
use DB;
use Illuminate\Http\JsonResponse;

class Controller extends ApiController
{
    public function sendToAccreditation(SendToAccreditationRequest $request): JsonResponse
    {
        return DB::transaction(function () use ($request) {
            $accreditation = app(SendToAccreditationAction::class)->run($request);
            return $this->created($this->transform($accreditation, AccreditationTransformer::class));
        });

    }

    public function checkAccreditation(CheckAccreditationRequest $request): JsonResponse
    {
        app(CheckAccreditationAction::class)->run($request);
        return $this->json('ok');
    }

    public function getModeratorAccreditations(GetModeratorAccreditationsRequest $request): JsonResponse
    {
        $accreditations = app(GetModeratorAccreditationsAction::class)->run($request);
        return $this->json($this->transform($accreditations, AccreditationTransformer::class));
    }

    public function getProfileAccreditations(int $id, GetProfileAccreditationsRequest $request): JsonResponse
    {
        $accreditations = app(GetProfileAccreditationsAction::class)->run($id, $request);
        return $this->json($this->transform($accreditations, AccreditationTransformer::class));
    }

    public function getAccreditation(int $id, GetAccreditationRequest $request): JsonResponse
    {
        $accreditation = app(GetAccreditationAction::class)->run($id, $request);
        return $this->json($this->transform($accreditation, AccreditationTransformer::class));
    }

    public function moderatorCheckAccreditation(int $id, ModeratorCheckAccreditationRequest $request): JsonResponse
    {
        return DB::transaction(function () use ($id, $request) {
            app(ModeratorCheckAccreditationAction::class)->run($id, $request);
            return $this->json('ok');
        });
    }

    public function updateAccreditation(int $id, UpdateAccreditationRequest $request): JsonResponse
    {
        $accreditation =  DB::transaction(function () use ($id, $request) {
            return app(UpdateAccreditationAction::class)->run($id, $request);
        });

        return $this->json($this->transform($accreditation, AccreditationTransformer::class));
    }
}
